package com.example.ximalayademo.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ximalayademo.R;
import com.example.ximalayademo.base.BaseApplication;
import com.example.ximalayademo.views.SobPopWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {

    private Context mContext;
    private int playingIndex = 0;

    List<Track> mTracks = new ArrayList<>();
    private Track mCurTrack;
    private SobPopWindow.PlayListItemClickListener mItemClickListener = null;

    public void setOnItemClickListener(SobPopWindow.PlayListItemClickListener clickListener) {
        this.mItemClickListener = clickListener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private final ImageView mPlayListImg;
        private final TextView mPlayListTitle;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            mPlayListImg = itemView.findViewById(R.id.play_icon);
            mPlayListTitle = itemView.findViewById(R.id.track_title_tv);
        }
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

        mCurTrack = mTracks.get(position);
        holder.mPlayListTitle.setTextColor(BaseApplication.getAppContext().getResources().getColor(playingIndex == position ?
                        R.color.main_color : R.color.play_list_text_color));

        holder.mPlayListTitle.setText(mCurTrack.getTrackTitle());

        holder.mPlayListImg.setVisibility(playingIndex == position ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getItemCount() {
        return mTracks.size();
    }


    public void setCurrentIndex(int position) {
        playingIndex = position;
        notifyDataSetChanged();
    }

    public void setData(List<Track> tracks) {
        mTracks.clear();
        mTracks.addAll(tracks);
        notifyDataSetChanged();
    }



}
